package com.fortune.service.impl;

import com.fortune.dao.DStoreSurplusDao;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusAddDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusEditDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusInfoDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusListDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.DStoreSurplusService;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfo;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfoDetail;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfoList;
import com.fortune.vo.dStoreTrans.DstoreTransDetail;
import com.fortune.vo.mStoreSurplus.StockDetailSurplus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;


@Service
public class DStoreSurplusServiceImpl implements DStoreSurplusService {
    @Autowired
    private DStoreSurplusDao dStoreSurplusDao;


    @Override
    public int addSurplusBase(String dealerId, String uid, DStoreSurplusAddDto addDto) {
        int num = dStoreSurplusDao.findNumIsNull(addDto.getNum());
        if(num > 0){
            return -1;
        }
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addSurplusBase=dStoreSurplusDao.addSurplusBase(dealerId,uuid,uid,addDto);
        if(addSurplusBase>0){
            int addSurplusDetail=0;
            for(DStoreSurplusInfoDto infoDto: addDto.getdStoreSurplusInfoDtos()){
                addSurplusDetail=dStoreSurplusDao.addSurplusDetail(dealerId,uuid,infoDto);
                if(addSurplusDetail<1){
                    throw new InsertInnerErrorException("新增报盈商品失败");
                }
            }
            return addSurplusDetail;
        }
        return addSurplusBase;
    }

    @Override
    public List<DStoreSurplusInfo> findSurplusList(String dealerId, DStoreSurplusListDto dStoreSurplusListDto) {
        SimpleDateFormat date =new SimpleDateFormat("yyyy-MM-dd");
        List<DStoreSurplusInfo> findSurplusList=dStoreSurplusDao.findDealerSurplusList(dealerId,dStoreSurplusListDto);
        for(DStoreSurplusInfo dStoreSurplusInfo:findSurplusList){
            if(dStoreSurplusInfo.getCreateTime()!=null){
                dStoreSurplusInfo.setCreatTimes(date.format(dStoreSurplusInfo.getCreateTime()));
            }
            if(dStoreSurplusInfo.getInTime()!=null) {
                dStoreSurplusInfo.setInTimes(date.format(dStoreSurplusInfo.getInTime()));
            }
        }
        return findSurplusList;
        }

    @Override
    public int findSurplusListCount(String dealerId, DStoreSurplusListDto dStoreSurplusListDto) {
        return dStoreSurplusDao.findDealerSurplusListCount(dealerId,dStoreSurplusListDto);
    }

    @Override
    public DStoreSurplusInfoDetail findDetail(String dealerId, String deficitId) {
        DStoreSurplusInfoDetail dStoreSurplusInfoDetail =  dStoreSurplusDao.findDealerIdSurplusInfo(dealerId,deficitId);
        for(DStoreSurplusInfoList ds : dStoreSurplusInfoDetail.getdStoreSurplusInfoList()){
            String mid = ds.getMid();
            if(ds.getMid().equals("")){
             ds.setmName("自营");
            }else{
               DStoreSurplusInfoList name = dStoreSurplusDao.findJXSname(ds.getMid());
               if(name == null){
                   ds.setmName("自营");
               }else{
                   ds.setmName(name.getmName());
               }
            }
        }
        return dStoreSurplusInfoDetail;
    }

    @Override
    @Transactional
    public int editDStoreSurplusConfirm(String dealerId,String operatorId, String uuid) {
        int addSurplusBase = dStoreSurplusDao.editDStoreSurplusConfirm(dealerId,operatorId, uuid);
        int no = 0;
        float kk;
        float rainNum;
        if(addSurplusBase>0){
            DStoreSurplusAddDto list = dStoreSurplusDao.findData(dealerId,uuid);
            int success = 0;
            for(DStoreSurplusInfoDto infoDto: list.getdStoreSurplusInfoDtos()){
                StockDetailSurplus num = dStoreSurplusDao.findProductStock(dealerId,list.getStoreId(),infoDto.getProductId(),infoDto.getMid());
                if(num == null){
                    no = 1;
                    rainNum = infoDto.getNum().floatValue();
                }else{
                    no = num.getNo() + 1;
                    kk = infoDto.getNum().floatValue();
                    rainNum = kk + num.getRemainNum();
                }
                success = dStoreSurplusDao.addProDuctStock(dealerId,list.getStoreId(),infoDto.getProductId(),no,uuid,infoDto.getNo(),infoDto.getNum(),rainNum,infoDto.getMid());
                if(success < 1){
                    throw new InsertInnerErrorException("新增库存明细账失败");
                }
            }
            return success;
        }else{
            throw new InsertInnerErrorException("新增库存明细账失败");
        }
    }

    @Override
    public int deleteDStoreSurplus(String dealerId, String uuid) {
        return dStoreSurplusDao.deleteDStoreSurplus(dealerId, uuid);
    }

    @Override
    public int editSurplusBase(String dealerId, DStoreSurplusEditDto editDto) {
        int editSurplusBase=dStoreSurplusDao.editSurplusBase(dealerId,editDto);
        if(editSurplusBase>0){
            String uuid=editDto.getUuid();
            int addSurplusDetail=0;
            int deleteInfo=dStoreSurplusDao.deleteInfo(dealerId,uuid);
            if(deleteInfo>0){
                for(DStoreSurplusInfoDto infoDto: editDto.getdStoreDeficitInfoDto()){
                    addSurplusDetail=dStoreSurplusDao.addSurplusDetail(dealerId,uuid,infoDto);
                    if(addSurplusDetail<1){
                        throw new InsertInnerErrorException("新增报盈商品失败");
                    }
                }
            }
            return addSurplusDetail;

        }
        return editSurplusBase;
    }
}
